<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Pack as PackModel;

class PackService
{
    /**
     * 礼包总数
     * @param $where
     * @return mixed
     */
    public static function countPack($where = [])
    {
        return PackModel::countPack($where);
    }

    /**
     * 本月礼包总数
     * @return int
     */
    public static function countPackWeek()
    {
        return PackModel::countPackWeek();
    }

    /**
     * 本周礼包总数
     * @return int
     */
    public static function countPackMonth()
    {
        return PackModel::countPackMonth();
    }

    /**
     * 礼包列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getPackList($where, $page, $pagesize)
    {
        $list = PackModel::getPackList($where, $page, $pagesize);
        foreach ($list as $k => $v) {
            $list[$k]['ids'] = $v['id'] . '<br><span class="color3">' . $v['uid'] . '</span>';

            $headimg = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $v['headimg'] . '" \' + \'alt=""></div>';
            // 身份
            $identity = UsersCateService::findUsersIdentity(['uid' => $v['uid']]);
            $identity = $identity ?: '游客';
//            $list[$k]['nickname_data'] = '<div class="d-fix1">' . $headimg . '<div><p class="datas">' . emojiDecode($v['nickname']) . '</p>';
            $list[$k]['nickname_data'] = emojiDecode($v['nickname']) . '<br><span class="color3">' . $identity . '</span>';

            // 推送次数
            $pushLog = ResourcePushLogService::countResourcePushLog(['uid' => $v['uid'], 'type' => 1]);
            $list[$k]['nums_data'] = $v['buy_nums'] . '<br><span class="color3">' . $pushLog . '</span>';

            $create_time = date('Y-m-d', strtotime($v['create_time']));
            $end_time = date('Y-m-d', $v['end_time']);
            $list[$k]['time_data'] = $create_time . '<br><span class="color4">' . $end_time . '</span>';
            $role = getRoleName($v['role']);
            $auth_city = $v['auth_city'] ?: '未知';
            $list[$k]['user_type'] = $auth_city . '<br><span class="color3">' . $role . '</span>';
        }

        return $list;
    }

    /**
     * 礼包列表统计
     * @param $where
     * @return mixed
     */
    public static function countPackList($where)
    {
        return PackModel::countPackList($where);
    }

    /**
     * 社群礼包地区分布
     * @return array
     */
    public static function getPackArea()
    {
        $data = PackModel::getPackArea();
        $datas = [];
        foreach ($data as $k => $v) {
            $datas[] = json_encode([
                'name' => $v['name'],
                'value' => $v['nums'],
            ], JSON_UNESCAPED_UNICODE);
        }
        $datas = join(';', $datas);
        // 剩余其他地区分布
        $lastData = PackModel::getPackArea();
        // 下单地区总数
        $totalArea = PackModel::countPackArea();
        return ['count' => count($data), 'datas' => $datas, 'lastData' => $lastData, 'totalArea' => $totalArea];
    }

    /**
     * 社群礼包购买人身份数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getIdentityPackData()
    {
        $peopleList = PackModel::getPackPeopleList();
        $identityData = [];
        foreach ($peopleList as $people) {
            $identity = UsersCateService::findUsersIdentity(['uid' => $people['uid']]);
            $identity = $identity ?: '游客';
            $identityData[] = $identity;
        }
        // 统计相同键的数据，组成新数组
        $valueArr = array_count_values($identityData);

        // 获取原身份数据
        $identityList = IdentityService::getIdentityList();
        $newArr = [];
        $colors = ['#FF6363FF', '#E6A500FF', '#8FDFFEFF', '#3793FAFF', '#233AE2FF', '#5B78FFFF', '#915BFFFF', '#FA32A3FF'];
        foreach ($identityList as $k => $v) {
            $nums = isset($valueArr[$v['name']]) ? $valueArr[$v['name']] : 0;
            $newArr[] = [
                'name'=>$v['name'],
                'nums'=>$nums,
                'colors'=>$colors[$k],
            ];
        }

        return $newArr;
    }

    /**
     * 单社群礼包
     * @param $where
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findPack($where)
    {
        $info = PackModel::findPack($where);
        if (!$info) {
            return false;
        }

        // 来源
        // 1公众号  2小程序
        $info['associate'] = $info['associate'] == 1 ? '公众号>' : '小程序';
        // 订单数据
        $orders = OrderService::findOrder(['uid' => $info['uid'], 'type_id' => 15, 'mid' => 7, 'status' => 1]);
        // 下单时间
        $info['pay_time'] = date('Y-m-d', $orders['pay_time']);
        // 支付金额
        $info['price'] = $orders['price'];
        // 用户身份
        $identity = UsersCateService::findUsersIdentity(['uid' => $info['uid']]);
        $info['identity'] = $identity ?: '游客';
        $info['role'] = getRoleName($info['role']);
        // 推送记录
        $pushLog = ResourcePushLogService::getResourcePushLog(['uid' => $info['uid'], 'type' => 1]);
        $info['countPushLog'] = count($pushLog);
        $newArr = [];
        foreach ($pushLog as $k => $v) {
            $count = count(explode(',', $v['ids']));
            $newArr[$k] = '<span class="color3">共推荐' . $count . '个社群</span><br><span>' . date('Y-m-d H:i', $v['push_time']) . '</span>';
        }
        $info['pushLog'] = $newArr;
        return $info;
    }


}